home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / fsusage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  5.1 KB  |  189 lines

  1. /* fsusage.c -- return space usage of mounted filesystems
  2.    Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #ifndef NO_INFOMOUNT
  22.  
  23. #include <sys/types.h>
  24.  
  25. #include "fsusage.h"
  26.  
  27. int statfs ();  /* We leave the type ambiguous intentionally here */
  28.  
  29. #ifdef HAVE_SYS_PARAM_H
  30. #include <sys/param.h>
  31. #endif
  32.  
  33. #ifdef HAVE_SYS_MOUNT_H
  34. #include <sys/mount.h>
  35. #endif
  36.  
  37. #ifdef HAVE_SYS_VFS_H
  38. #include <sys/vfs.h>
  39. #endif
  40.  
  41. #ifdef HAVE_SYS_FILSYS_H
  42. #include <sys/filsys.h>        /* SVR2.  */
  43. #endif
  44.  
  45. #ifdef HAVE_FCNTL_H
  46. #include <fcntl.h>
  47. #endif
  48.  
  49. #ifdef HAVE_SYS_STATFS_H
  50. #include <sys/statfs.h>
  51. #endif
  52.  
  53. #ifdef HAVE_DUSTAT_H        /* AIX PS/2.  */
  54. #include <sys/dustat.h>
  55. #endif
  56.  
  57. #ifdef HAVE_SYS_STATVFS_H    /* SVR4.  */
  58. #include <sys/statvfs.h>
  59. #endif
  60.  
  61. /* Return the number of TOSIZE-byte blocks used by
  62.    BLOCKS FROMSIZE-byte blocks, rounding away from zero.
  63.    TOSIZE must be positive.  Return -1 if FROMSIZE is not positive.  */
  64.  
  65. long fs_adjust_blocks (long blocks, int fromsize, int tosize)
  66. {
  67.     if (tosize <= 0)
  68.     abort ();
  69.     if (fromsize <= 0)
  70.     return -1;
  71.  
  72.     if (fromsize == tosize)    /* E.g., from 512 to 512.  */
  73.     return blocks;
  74.     else if (fromsize > tosize)    /* E.g., from 2048 to 512.  */
  75.     return blocks * (fromsize / tosize);
  76.     else            /* E.g., from 256 to 512.  */
  77.     return (blocks + (blocks < 0 ? -1 : 1)) / (tosize / fromsize);
  78. }
  79.  
  80. /* Fill in the fields of FSP with information about space usage for
  81.    the filesystem on which PATH resides.
  82.    Return 0 if successful, -1 if not. */
  83.  
  84. int get_fs_usage (char *path, struct fs_usage *fsp)
  85. {
  86. #ifdef STAT_STATFS3_OSF1
  87.     struct statfs fsd;
  88.  
  89.     if (statfs (path, &fsd, sizeof (struct statfs)) != 0)
  90.      return -1;
  91. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
  92. #endif                /* STAT_STATFS3_OSF1 */
  93.  
  94. #ifdef STAT_STATFS2_FS_DATA    /* Ultrix.  */
  95.     struct fs_data fsd;
  96.  
  97.     if (statfs (path, &fsd) != 1)
  98.     return -1;
  99. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), 1024, 512)
  100.     fsp->fsu_blocks = CONVERT_BLOCKS (fsd.fd_req.btot);
  101.     fsp->fsu_bfree = CONVERT_BLOCKS (fsd.fd_req.bfree);
  102.     fsp->fsu_bavail = CONVERT_BLOCKS (fsd.fd_req.bfreen);
  103.     fsp->fsu_files = fsd.fd_req.gtot;
  104.     fsp->fsu_ffree = fsd.fd_req.gfree;
  105. #endif
  106.  
  107. #ifdef STAT_STATFS2_BSIZE    /* 4.3BSD, SunOS 4, HP-UX, AIX.  */
  108.     struct statfs fsd;
  109.  
  110.     if (statfs (path, &fsd) < 0)
  111.     return -1;
  112. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
  113. #endif
  114.  
  115. #ifdef STAT_STATFS2_FSIZE    /* 4.4BSD.  */
  116.     struct statfs fsd;
  117.  
  118.     if (statfs (path, &fsd) < 0)
  119.     return -1;
  120. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_fsize, 512)
  121. #endif
  122.  
  123. #ifdef STAT_STATFS4        /* SVR3, Dynix, Irix, AIX.  */
  124.     struct statfs fsd;
  125.  
  126.     if (statfs (path, &fsd, sizeof fsd, 0) < 0)
  127.     return -1;
  128.     /* Empirically, the block counts on most SVR3 and SVR3-derived
  129.        systems seem to always be in terms of 512-byte blocks,
  130.        no matter what value f_bsize has.  */
  131. #if _AIX
  132. #define CONVERT_BLOCKS(b) fs_adjust_blocks ((b), fsd.f_bsize, 512)
  133. #else
  134. #define CONVERT_BLOCKS(b) (b)
  135. #ifndef _SEQUENT_        /* _SEQUENT_ is DYNIX/ptx.  */
  136. #ifndef DOLPHIN            /* DOLPHIN 3.8.alfa/7.18 has f_bavail */
  137. #define f_bavail f_bfree
  138. #endif
  139. #endif
  140. #endif
  141. #endif
  142.  
  143. #ifdef STAT_STATVFS        /* SVR4.  */
  144.     struct statvfs fsd;
  145.  
  146.     if (statvfs (path, &fsd) < 0)
  147.     return -1;
  148.     /* f_frsize isn't guaranteed to be supported.  */
  149. #define CONVERT_BLOCKS(b) \
  150.   fs_adjust_blocks ((b), fsd.f_frsize ? fsd.f_frsize : fsd.f_bsize, 512)
  151. #endif
  152.  
  153. #if defined(CONVERT_BLOCKS) && !defined(STAT_STATFS2_FS_DATA) && !defined(STAT_READ_FILSYS)    /* !Ultrix && !SVR2.  */
  154.     fsp->fsu_blocks = CONVERT_BLOCKS (fsd.f_blocks);
  155.     fsp->fsu_bfree = CONVERT_BLOCKS (fsd.f_bfree);
  156.     fsp->fsu_bavail = CONVERT_BLOCKS (fsd.f_bavail);
  157.     fsp->fsu_files = fsd.f_files;
  158.     fsp->fsu_ffree = fsd.f_ffree;
  159. #endif
  160.  
  161.     return 0;
  162. }
  163.  
  164. #if defined(_AIX) && defined(_I386)
  165. /* AIX PS/2 does not supply statfs.  */
  166.  
  167. int statfs (char *path, struct statfs *fsb)
  168. {
  169.     struct stat stats;
  170.     struct dustat fsd;
  171.  
  172.     if (stat (path, &stats))
  173.     return -1;
  174.     if (dustat (stats.st_dev, 0, &fsd, sizeof (fsd)))
  175.     return -1;
  176.     fsb->f_type = 0;
  177.     fsb->f_bsize = fsd.du_bsize;
  178.     fsb->f_blocks = fsd.du_fsize - fsd.du_isize;
  179.     fsb->f_bfree = fsd.du_tfree;
  180.     fsb->f_bavail = fsd.du_tfree;
  181.     fsb->f_files = (fsd.du_isize - 2) * fsd.du_inopb;
  182.     fsb->f_ffree = fsd.du_tinode;
  183.     fsb->f_fsid.val[0] = fsd.du_site;
  184.     fsb->f_fsid.val[1] = fsd.du_pckno;
  185.     return 0;
  186. }
  187. #endif                /* _AIX && _I386 */
  188. #endif /* NO_INFOMOUNT */
  189.